package com.kzj.service.impl;

import com.kzj.common.constant.GlobalConstant;
import com.kzj.common.enumerate.RoleEnum;
import com.kzj.common.result.Result;
import com.kzj.common.util.InheritableThreadLocalUtil;
import com.kzj.common.util.PasswordUtil;
import com.kzj.pojo.Teacher;
import com.kzj.pojo.dto.ChangePasswordDto;
import com.kzj.pojo.dto.LoginDto;
import com.kzj.pojo.dto.TeacherUpdateDto;
import com.kzj.pojo.vo.TeacherVo;
import com.kzj.service.TeacherService;
import com.kzj.service.jpaRepository.TeacherJpaRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherJpaRepository teacherJpaRepository;

    public Result login(LoginDto dto,HttpSession session) {
        if(!dto.getVerifyCode().equals(session.getAttribute(GlobalConstant.VERIFY_CODE)))
            return Result.error("验证码错误");
        Teacher teacher = teacherJpaRepository.getTeachersByNo(dto.getUsername());
        if(teacher==null)
            return Result.error("账号不存在");
        if(!PasswordUtil.checkPassword(teacher.getPassword(),dto.getPassword(),teacher.getSalt()))
            return Result.error("密码错误");
        session.setAttribute(GlobalConstant.ID,teacher.getId());
        session.setAttribute(GlobalConstant.ROLE, RoleEnum.TEACHER);
        return Result.success(teacher.getId());
    }


    public Result getInfo(HttpSession session) {
        Long id = (Long) session.getAttribute(GlobalConstant.ID);
        return Result.success(getVo(teacherJpaRepository.getById(id)));
    }

    @Override
    public Result getAll() {
        return Result.success(getVo(teacherJpaRepository.findAll()));
    }

    @Override
    public Result upate(TeacherUpdateDto dto) {
        Teacher teacher = teacherJpaRepository.getById(dto.getId());
        BeanUtils.copyProperties(dto,teacher);
        teacherJpaRepository.save(teacher);
        return Result.success();
    }

    @Override
    public Result changePassword(ChangePasswordDto dto,HttpSession session) {
        Long id =(Long) session.getAttribute(GlobalConstant.ID);
        Teacher teacher=teacherJpaRepository.findById(id).get();
        if(!dto.getNewPassword().equals(dto.getNewPasswordAgain()))
            return Result.error("两次输入的新密码不一致");
        if(!PasswordUtil.checkPassword(teacher.getPassword(),dto.getOldPassword(),teacher.getSalt()))
            return Result.error("密码错误");
        String password = PasswordUtil.encodeBySalt(dto.getNewPassword(), teacher.getSalt());
        if(password.equals(teacher.getPassword()))
            return Result.error("新旧密码不能相同");
        teacher.setPassword(password);
        teacherJpaRepository.save(teacher);
        return Result.success();
    }

    public TeacherVo getVo(Teacher teacher)
    {
        TeacherVo vo = new TeacherVo();
        BeanUtils.copyProperties(teacher,vo);
        return vo;
    }
    public List<TeacherVo> getVo(List<Teacher> teachers)
    {
        return teachers.stream().map(this::getVo).collect(Collectors.toList());
    }
}
